#include "Network.h"

Network::Network(void)
{
	mClient = 0;

	//default time out 10s
	timeOut = 10;
}

//--------------------------------------------------------------

Network::~Network(void)
{
	if(mClient!=0)
	{
		delete mClient;
		mClient = 0;
	}
}

//--------------------------------------------------------------

bool Network::connect(Ogre::String hostAddress, int port)
{
	try {

	std::cout << "\nNetwork<connect>: connecting to server: " << hostAddress.data() << ":" << port;
	mClient = new SocketClient(hostAddress.data(),port);
	std::cout << "\nNetwork<connect>: connection estabilished!";
	return true;

	}
	catch (const char* s) 
	{
		std::cout << "\nNetwork<connect>: " << s << endl;
		return false;
    } 
    catch (std::string s) 
	{
       
		std::cout << "\nNetwork<connect>: " << s << endl;
		return false;
    } 
    catch (...) 
	{
        std::cout << "\nNetwork<connect>: " << "unhandled exception\n";
		return false;
    }

}


//------------------------------------------------------------------

bool Network::sendString(Ogre::String message)
{
	if(mClient==0)
	{
		return false;
	}
	else
	{
			try {

			std::cout << "\nNetwork<send>: sending data to server...";
			mClient->SendLine(message.data());		
			std::cout << "\nNetwork<send>: data succesfully sent to server...";
			return true;

			}
			catch (const char* s) 
			{
				std::cout << "\nNetwork<send>: " << s << endl;
				return false;
			} 
			catch (std::string s) 
			{
       
				std::cout << "\nNetwork<send>: " << s << endl;
				return false;
			} 
			catch (...) 
			{
				std::cout << "\nNetwork<send>: " << "unhandled exception\n";
				return false;
			}
	}
}


//-------------------------------------------------------------------------------------

Ogre::String Network::receiveString(void)
{
	if(mClient==0)
	{
		return "<Error>";
	}
	else
	{
			try {

			std::cout << "\nNetwork<receive>: receiving data from server...";
			
			time_t startTime = time(NULL);
			time_t endTime;
			int ellapsedTime = 0;

			while (ellapsedTime < timeOut) 
				{
				  string l = mClient->ReceiveLine();
				  if (l.empty())
				  {
					  std::cout << "\nNetwork<receive>: received empty string from server...";
					  return "<Error>";
				  }
				  else
				  {
					  std::cout << "\nNetwork<receive>: data succesfully received from server!";
					  return l;
				  }

				  endTime = time(NULL);
				  ellapsedTime = endTime - startTime;
				}

			std::cout << "\nNetwork<receive>: timeout while waiting to server...";
			return "<Error>";

			}
			catch (const char* s) 
			{
				std::cout << "\nNetwork<receive>: " << s << endl;
				return "<Error>";
			} 
			catch (std::string s) 
			{
       
				std::cout << "\nNetwork<receive>: " << s << endl;
				return "<Error>";
			} 
			catch (...) 
			{
				std::cout << "\nNetwork<receive>: " << "unhandled exception\n";
				return "<Error>";
			}
	}
}